package com.gcstorage.tvdev.util;


import android.os.Build;

import androidx.annotation.RequiresApi;

import com.gcstorage.tvdev.bean.SplitFileBean;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auth mrz
 * @data 2021/08/10
 * @description 文件上传大小限制分片上传
 */
public class FileUploadSplitUtil {

    /**
     * 文件单次上传大小限制
     */
    private static final int MAX_SIZE = 5 * 1024 * 1024;

    /**
     * @param paths 路径
     * @return 文件大小是否超限
     */
    public static boolean fileOverLimit(List<String> paths){
        if (paths.isEmpty()){
            return false;
        }
        try {
            long totalSize = paths.stream().mapToLong(s -> new File(s).length()).sum();
            return totalSize > MAX_SIZE;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 默认到此方法，数据已超限
     * @param paths 路径
     * @return 获取分片数据
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public ArrayList<SplitFileBean> getSpiltFiles(List<String> paths){
        ArrayList<SplitFileBean> list = new ArrayList<>();
        try {
            paths.forEach( path -> {
                File file = new File(path);
                if (file.length() <= MAX_SIZE){
                    SplitFileBean splitFileBean = new SplitFileBean();
                    splitFileBean.setData(getBytesByFile(file));
                    splitFileBean.setIndex(0);
                    splitFileBean.setPartCount(1);
                    splitFileBean.setTime(String.valueOf(System.currentTimeMillis()));
                    splitFileBean.setFileName(file.getName());
                    list.add(splitFileBean);
                }else {
                    //文件需要分片
                    try {
                        String fileName = file.getName();
                        FileInputStream fis = new FileInputStream(file);
                        ByteArrayOutputStream bos = new ByteArrayOutputStream(MAX_SIZE);
                        String time = String.valueOf(System.currentTimeMillis());
                        byte[] b = new byte[MAX_SIZE];
                        int index = 0;
                        int n;
                        int totalSize = (int)(file.length() / MAX_SIZE) + 1;
                        while ((n = fis.read(b)) != -1) {
                            bos.write(b, 0, n);
                            SplitFileBean splitFileBean = new SplitFileBean();
                            splitFileBean.setFileName(fileName);
                            splitFileBean.setIndex(index);
                            splitFileBean.setTime(time);
                            splitFileBean.setPartCount(totalSize);
                            splitFileBean.setData(bos.toByteArray());
                            bos.reset();
                            index++;
                            list.add(splitFileBean);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }



    public static byte[] getBytesByFile(File file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            bos.flush();
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
